﻿@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="800">
    <MDCard>
        <MCardTitle Class="text-h5">
            编辑发行版本
        </MCardTitle>
        <MCardText>
            发行版本包含名称、日期、平台、链接......
        </MCardText>
        <MCardText Class="pb-0">
            <div class="row">
                <div class="col-12  col-sm-6">
                    <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.Type" TValue="GameReleaseType" Label="类型" />
                </div>
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.Name" Label="游戏发行名称" />
                </div>
                <div class="col-12  col-sm-6">
                    <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.PublishPlatformType" TValue="PublishPlatformType" Label="发行平台类型" />
                </div>
                @if (Model.PublishPlatformType == PublishPlatformType.Other)
                {
                    <div class="col-12  col-sm-6">
                        <MTextField @bind-Value="Model.PublishPlatformName" Label="发行平台名称" />
                    </div>
                }
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.Link" Label="@(Model.PublishPlatformType== PublishPlatformType.Other?"链接":"平台Id")" />
                </div>
                <div class="col-12">
                    <CnGalWebSite.Components.Checkboxes.InputEnumCheckboxList Items="Model.GamePlatformTypes" TValue="GamePlatformType" Label="游戏平台" />
                </div>
                <div class="col-12  col-sm-6">
                    <CnGalWebSite.Components.Inputs.MasaDatePicker @bind-NullableValue="Model.Time" Nullable Label="发行时间" ShowDate ShowTime="false" />
                </div>
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.TimeNote" Label="发行时间备注" />
                </div>
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.Engine" Label="引擎" />
                </div>
            </div>
        </MCardText>
        <MCardActions Class="pb-6 pe-6 ps-6">
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="保存" IsAsync="true" OnClick="OnSave" Icon="mdi-check" Color="success" TextStyle />
        </MCardActions>
    </MDCard>
</MDialog>
@code {

    private EditReleaseModel Model { get; set; }
    private EditReleaseModel _model { get; set; }
    [Parameter]
    public List<EditReleaseModel> Items { get; set; }
    [Parameter]
    public EventCallback OnChanged { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool Dialog { get; set; }

    private async Task OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            await OnSave();
        }
    }

    public void Refresh(EditReleaseModel model)
    {
        _model = model ?? new EditReleaseModel();
        Model = new EditReleaseModel();
        Model.SynchronizationProperties(_model);
        Dialog = true;

        StateHasChanged();

    }

    public async Task OnSave()
    {
        if (string.IsNullOrWhiteSpace(Model.Name))
        {
            await PopupService.ToastAsync("请填写游戏发行名称", AlertTypes.Error);
            return;
        }
        if (Items.Any(s => s != _model && s.PublishPlatformType == Model.PublishPlatformType && s.PublishPlatformName == Model.PublishPlatformName && s.Link == Model.Link && s.Name == Model.Name))
        {
            await PopupService.ToastAsync("平台和链接必须保持唯一", AlertTypes.Error);
            return;
        }
        if (Items.Any(s => s == _model) == false)
        {
            Items.Add(_model);
        }
        _model.SynchronizationProperties(Model);

        Dialog = false;
        StateHasChanged();
        await OnChanged.InvokeAsync();
    }

}
